cmake_minimum_required(VERSION 3.31)
project(qt_window)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "F:/qt/QT/6.9.1/mingw_64")

find_package(Qt6 COMPONENTS
        Core
        Gui
        Widgets
        REQUIRED)

# 跨平台隐藏控制台配置
if(WIN32)
    # Windows平台：设置为窗口应用
    set(MyQtApp WIN32_EXECUTABLE ON)
elseif(APPLE)
    # macOS平台：设置为应用程序包
    set(MyQtApp MACOSX_BUNDLE ON )
else()
    set (MyQtApp )
    # Linux平台：默认通过Qt窗口模块隐式处理，无需额外参数
    # 若需进一步控制，可添加编译选项（见注释）
    # target_compile_options(MyQtApp PRIVATE -Wl,--no-as-needed -Wl,-e,main)
endif()
# 添加资源文件
qt_add_resources(RESOURCES resources.qrc)
if(WIN32)
    # 添加 .rc 文件
    set(WIN32_RESOURCES app.rc)
endif ()

if(APPLE)
    set(MACOSX_BUNDLE_ICON_FILE app.icns)
    set(APP_ICON_MACOS ${CMAKE_CURRENT_SOURCE_DIR}/icons/app.icns
            ImageView.cpp
            ImageView.h)
    set_source_files_properties(${APP_ICON_MACOS} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
    set(MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist)
    # add_executable(qt_window MACOSX_BUNDLE main.cpp ${RESOURCES} ${APP_ICON_MACOS})
endif ()

add_executable(qt_window main.cpp
        ftwindow.cpp
        ftwindow.h
        ftwindow.ui ${RESOURCES}  ${WIN32_RESOURCES}  ${APP_ICON_MACOS}
        ImageView.cpp
        ImageView.h)

target_link_libraries(qt_window
        Qt::Core
        Qt::Gui
        Qt::Widgets
)
if (WIN32  OR APPLE)
    set_target_properties(qt_window PROPERTIES ${MyQtApp})
endif ()




if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt6${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()
add_executable(person test/Person.cpp test/Person.h)
